//
//  LMDetailController.m
//  EPA
//
//  Created by lanou3g on 16/4/22.
//  Copyright © 2016年 liman. All rights reserved.
//

#import "LMDetailController.h"
#import "LMNetWorkRequestManager.h"
@interface LMDetailController ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;


@end

@implementation LMDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.title;
    self.webView = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
    [self loadDataFormID:self.contentId];
    
  
}

-(void)loadDataFormID:(NSString *)ID{
    NSDictionary *params = [NSDictionary dictionary];
    if ([UserInfoManager shareManager].auth) {
        params =@{@"contentid":ID,@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":[UserInfoManager shareManager].auth,@"version":@"3.0.2"};
    }else{
        params = @{@"contentid":ID,@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":@"",@"version":@"3.0.2"};
    }
    [LMNetWorkRequestManager requestURL:[NSURL URLWithString:TOPICINFO_URL] Method:LM_POST Params:params Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDict = jsonDict[@"data"];
        NSDictionary *postsinfoDict = dataDict[@"postsinfo"];
        NSString *htnlStr = postsinfoDict[@"html"];
        NSLog(@"%@***************",dataDict);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.webView loadHTMLString:htnlStr baseURL:nil];
        });
    }];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"加载完成");
    
    [webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth=380;" //缩放系数
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "if(myimg.width > maxwidth){"
     "oldwidth = myimg.width;"
     "myimg.width = maxwidth;"
     "myimg.height = myimg.height * (maxwidth/oldwidth);"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
